package edu.pku.sei.mdabase.infrastructure.graph;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


@SuppressWarnings("unchecked")
public class GraphNode<EDGE extends GraphEdge,GRAPH extends Graph> extends GraphObject<GRAPH> {


	private HashMap<Object,List<EDGE>> edgesCache = new HashMap<Object,List<EDGE>>();
	
	public void setEdgeCacheDirtyFlag(Object key){
		List<EDGE> edges = edgesCache.get(key);
		if(edges!=null){
			edges.clear();
		}
	}
	
	public boolean isEdgeChanged(Object key){
		List<EDGE> edges = edgesCache.get(key);
		return edges==null;
	}
	
	
	public List<EDGE> getEdges(){
		return getEdges(Graph.BASE);
	}
	
	public List<EDGE> getEdges(Object key){
		List<EDGE> edges = edgesCache.get(key);
		
		if(edges==null) {
			edges = new ArrayList<EDGE>();
			edgesCache.put(key, edges);
		}
		if(edges.isEmpty()) {
			edges.addAll(getIncomings(key));
			edges.addAll(getOutgoings(key));
		}
		
		final List<EDGE> ret = edges;
		
		return ret;
	}
	

	public List<EDGE> getIncomings(){
		return  getIncomings(Graph.BASE);
	}
	public List<EDGE> getIncomings(Object key) {
		return this.getHost(key).getNodeIncomings(this);
	}
	
	public List<EDGE> getOutgoings(){
		return  getOutgoings(Graph.BASE);
	}
	public List<EDGE> getOutgoings(Object key) {
		return this.getHost(key).getNodeOutgoings(this);
	}

}
